# file:: $Name$
# author::  $Author$
# version:: $Revision$
# date:: $Date$
#
# This source code copyright (C) 2006 by Ralph M. Churchill
# All rights reserved.
#
# Released under the terms of the GNU General Public License
# See LICENSE file for additional information.

require 'engine/command'

class Builder < Command
    def initialize(executor)
        super(executor,"exec","See HELP",
              "Do something amazing!")
    end
    def execute(params)
        raise UsageException if params.empty?
        where = params.join(' ').split /in\(|\):/
        on = 'room'
        if where.size > 1
            on,params = where[1..2]
        end
        obj_typ, obj_name, cmd = params.split(/\(|\)/)

        search = case obj_typ.strip
        when 'i'
            (on =~ /room/) ? \
                self.executor.room.items : self.executor.items
        when 'c'
            (on =~ /room/) ? \
                self.executor.room.characters: [self.executor]
        end

        obj = search.flatten.find{|e| e.named? obj_name}
        unless obj.nil?
            begin
                cmd.sub!(/^./,'')
                obj.instance_eval("#{cmd}")
            rescue => e
                inform("Error sending #{cmd} to #{obj}: #{e}")
            end
        else
            inform("Could not find #{obj_name}")
        end
    end
end
